package com.macro.mall.tiny.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.dto.ProductCategoryParam;
import com.macro.mall.tiny.mbg.model.PmsProductCategory;
import com.macro.mall.tiny.service.PmsProductCategoryService;
import com.macro.mall.tiny.vo.PmsProductCategoryWithChildrenItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags="PmsProductCategory")
@Tag(name="PmsProductCategory",description = "商品分类管理")
@Slf4j
@RequestMapping("/productCategory")
@PreAuthorize("@ex.hasAuthority('/productCategory')")
public class PmsProductCategoryController {

    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    //添加商品分类
    @PostMapping("/create")
    @ApiOperation("添加商品分类")
    public CommonResult create(@Validated @RequestBody ProductCategoryParam productCategoryParam){
        log.info("添加商品分类:{}",productCategoryParam);

        Integer code=pmsProductCategoryService.create(productCategoryParam);
        return code>0? CommonResult.success():CommonResult.failed();

    }

    //修改商品分类
    @PostMapping("/update/{id}")
    @ApiOperation("修改商品分类")
    public CommonResult update(@PathVariable Long id,@RequestBody ProductCategoryParam productCategoryParam){
        log.info("修改商品分类：{}，{}",id,productCategoryParam);

        Integer code=pmsProductCategoryService.update(id,productCategoryParam);
        return code>0? CommonResult.success():CommonResult.failed();
    }

    //分页查询商品分类
    @GetMapping("/list/{parentId}")
    @ApiOperation("分页查询商品分类")
    public CommonResult<CommonPage<PmsProductCategory>> getList(
            @PathVariable Long parentId,
            @RequestParam(value="pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value="pageSize",defaultValue = "5")Integer pageSize){
        log.info("分页查询商品分类:{},{},{}",parentId,pageNum,pageSize);

        List<PmsProductCategory> pmsProductCategoryList=pmsProductCategoryService.getList(parentId,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(pmsProductCategoryList));
    }

    //根据id获取商品分类
    @GetMapping("/{id}")
    @ApiOperation("根据id获取商品分类")
    public CommonResult<PmsProductCategory> getById(@PathVariable Long id){
        log.info("根据id获取商品分类：{}",id);

        PmsProductCategory pmsProductCategory=pmsProductCategoryService.getById(id);
        return CommonResult.success(pmsProductCategory);
    }

    //删除商品分类
    @PostMapping("/delete/{id}")
    @ApiOperation("删除商品分类")
    public CommonResult deleteById(@PathVariable Long id){
        log.info("删除商品分类:{}",id);

        Integer code=pmsProductCategoryService.deleteById(id);
        return code==1? CommonResult.success():CommonResult.failed();
    }

    //修改导航栏显示状态
    @PostMapping("/update/{navStatus}")
    @ApiOperation("修改导航栏显示状态")
    public CommonResult updateNavStatus(
            @RequestParam(value="ids") List<Long> ids,
            @RequestParam(value="navStatus") Integer navStatus){
        log.info("修改导航栏显示状态:{},{}",ids,navStatus);

        Integer code=pmsProductCategoryService.updateNavStatus(ids,navStatus);
        return code==ids.size()? CommonResult.success():CommonResult.failed("部分或者全部修改失败");
    }

    //修改显示状态
    @PostMapping("/update/showStatus")
    @ApiOperation("修改显示状态")
    public CommonResult updateShowStatus(
            @RequestParam(value="ids") List<Long> ids,
            @RequestParam(value="showStatus") Integer showStatus){
        log.info("修改显示状态:{},{}",ids,showStatus);

        Integer code=pmsProductCategoryService.updateShowStatus(ids,showStatus);
        return code==ids.size()? CommonResult.success():CommonResult.failed("部分或者全部修改失败");
    }

    //查询所有一级分类及其子类
    @GetMapping("/list/withChildren")
    @ApiOperation("查询所有一级分类及其子类")
    public CommonResult<List<PmsProductCategoryWithChildrenItem>>listWithChildren(){
        log.info("查询所有一级分类及其子类");

        List<PmsProductCategoryWithChildrenItem> list=pmsProductCategoryService.listWithChildren();
        return CommonResult.success(list);
    }




}
